package com.zpark.interceptor;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.zpark.util.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @PackageName: com.zpark.interceptor
 * @ClassName: UserInterceptor
 * @Description:
 * @author: RZS
 * @date: 2024/5/15  11:50
 */
public class UserInterceptor implements HandlerInterceptor {

    // 拦截方法，返回true：放行，false：拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 实现拦截的逻辑
         */
        // 获取token
        String token = request.getHeader("Authorization");
        // 校验token
        try {
            DecodedJWT decodedJWT = JwtUtil.verifyToken(token);
            // 放行
            return true;
        } catch(Exception e) {
            // token无效，重定向到登录页面
            // response.sendRedirect("....");
            e.printStackTrace();
            System.err.println("请求被拦截，没有登录");
            return false;
        }
    }


}
